/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.dp1.pucp.Compras.DAO;

import com.dp1.pucp.Modelo.Clases.Costoinsumos;
import com.dp1.pucp.Modelo.Clases.Kardex;
import com.dp1.pucp.Modelo.Clases.Lineaordencompra;
import com.dp1.pucp.Modelo.Clases.Lineapedidocompra;
import com.dp1.pucp.Modelo.Clases.Materiaprima;
import com.dp1.pucp.Modelo.Clases.Moneda;
import com.dp1.pucp.Modelo.Clases.Ordencompra;
import com.dp1.pucp.Modelo.Clases.Pedidocompra;
import com.dp1.pucp.Modelo.Clases.Simulacion;
import com.dp1.pucp.Modelo.Clases.Tipoproducto;
import com.dp1.pucp.Util.HibernateUtil;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import org.hibernate.Hibernate;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 *
 * @author ubuntu64
 */
public class OrdenCompraDAO {
    
//    public void guardarOrdenCompra(Ordencompra ordencompra){
//        Session session = HibernateUtil.getSessionFactory().openSession();
//        Transaction tx =  session.beginTransaction();
//        try {
//            session.saveOrUpdate(ordencompra);
//            tx.commit();
//        }catch(HibernateException e) {
//            throw new HibernateException(e);
//        } finally {
//            session.close();            
//        }        
//    }
    public ArrayList<String> listarEstado() 
    {
        ArrayList<String> estados = new ArrayList<>();
        estados.add("Seleccionar");
        estados.add("Eliminado");
        estados.add("Registrado");
        estados.add("Pendiente");
        estados.add("Recibido");
        return estados;
    }
    
    public ArrayList<String> listarEstadoAux() 
    {
        ArrayList<String> estados = new ArrayList<>();
        estados.add("--Seleccione--");
//        estados.add("Eliminado");
        estados.add("Registrado");
        estados.add("Pendiente");
//        estados.add("Recibido");
        return estados;
    }
     public Ordencompra guardarOrdenCompra(Ordencompra ordencompra){
        Ordencompra c = new Ordencompra();
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction tx =  session.beginTransaction();
        try {
            session.saveOrUpdate(ordencompra);
            session.flush();
         
        }catch(HibernateException e) {
            throw new HibernateException(e);
        } finally {
            
            session.persist(ordencompra);
            
            int cod = ordencompra.getIdordencompra();
            
            List lista = session.createQuery("from Ordencompra where idordencompra="+cod).list();        
            Iterator iter = lista.iterator();
            while (iter.hasNext()) { 
                c = (Ordencompra) iter.next();
            }        
            tx.commit();
            session.close();
            return c;
        }
        
    }
     
     public void guardarSimulacion(Simulacion simulacion){
        Simulacion c = new Simulacion();
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction tx =  session.beginTransaction();
        try {
            session.saveOrUpdate(simulacion);
            session.flush();
         
        }catch(HibernateException e) {
            throw new HibernateException(e);
        } finally {
            
            session.persist(simulacion);
            
            int cod = simulacion.getIdsimulacion();
            
            List lista = session.createQuery("from Simulacion where idsimulacion="+cod).list();        
            Iterator iter = lista.iterator();
            while (iter.hasNext()) { 
                c = (Simulacion) iter.next();
            }        
            tx.commit();
            session.close();
            
        }
        
    }
     
     public void guardarPedidoCompra(Pedidocompra pedidocompra){
        Pedidocompra c = new Pedidocompra();
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction tx =  session.beginTransaction();
        try {
            session.saveOrUpdate(pedidocompra);
            session.flush();
         
        }catch(HibernateException e) {
            throw new HibernateException(e);
        } finally {
            
            session.persist(pedidocompra);
            
            int cod = pedidocompra.getIdpedidocompra();
            
            List lista = session.createQuery("from Pedidocompra where idpedidocompra="+cod).list();        
            Iterator iter = lista.iterator();
            while (iter.hasNext()) { 
                c = (Pedidocompra) iter.next();
            }        
            tx.commit();
            session.close();
            
        }
        
    }
     
     public Lineaordencompra guardarLineaOrdenCompra(Lineaordencompra lineaordencompra){
        Lineaordencompra c = new Lineaordencompra();
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction tx =  session.beginTransaction();
        try {
            session.saveOrUpdate(lineaordencompra);
            session.flush();
         
        }catch(HibernateException e) {
            throw new HibernateException(e);
        } finally {
            
            session.persist(lineaordencompra);
            
            int cod = lineaordencompra.getIdlineaordencompra();
            
            List lista = session.createQuery("from Lineaordencompra where idlineaordencompra="+cod).list();        
            Iterator iter = lista.iterator();
            while (iter.hasNext()) { 
                c = (Lineaordencompra) iter.next();
            }        
            tx.commit();
            session.close();
            return c;
        }
        
    }
    
     public void guardarLineaPedidoCompra(Lineapedidocompra lineapedidocompra){
        Lineapedidocompra c = new Lineapedidocompra();
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction tx =  session.beginTransaction();
        try {
            session.saveOrUpdate(lineapedidocompra);
            session.flush();
         
        }catch(HibernateException e) {
            throw new HibernateException(e);
        } finally {
            
            session.persist(lineapedidocompra);
            
            int cod = lineapedidocompra.getIdlineapedidocompra();
            
            List lista = session.createQuery("from Lineapedidocompra where Idlineapedidocompra="+cod).list();        
            Iterator iter = lista.iterator();
            while (iter.hasNext()) { 
                c = (Lineapedidocompra) iter.next();
            }        
            tx.commit();
            session.close();
            
        }
        
    }
    
    public ArrayList<Ordencompra> listarOrdenPorCodigo(String codigo)
    {
        ArrayList<Ordencompra> listaResultados = new ArrayList<>();       
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = session.beginTransaction();
        try{
            List lista = session.createQuery("from Ordencompra where codigo = '"+ codigo +"' order by idordencompra").list();        
            Iterator iter = lista.iterator();
            while (iter.hasNext()) {    
                Ordencompra p = (Ordencompra) iter.next();
                Hibernate.initialize(p.getCodigo());
                Hibernate.initialize(p.getEstado());
                Hibernate.initialize(p.getFecharecepcion());                
                Hibernate.initialize(p.getFechasolicitud());
                Hibernate.initialize(p.getIdordencompra());                
                Hibernate.initialize(p.getLineaordencompras()); 
                Hibernate.initialize(p.getMoneda());                
                Hibernate.initialize(p.getMontototal());
                Hibernate.initialize(p.getProveedor());
                 Hibernate.initialize(p.getProveedor().getRazonsocial());           
                listaResultados.add(p);
            }        
            tx.commit();
        } catch(HibernateException e) {
            throw new HibernateException(e);    
        } finally {
            session.close();
            return listaResultados;
        }         
    }   
    
    public ArrayList<Ordencompra> listarOrdenPorId(int id)
    {
        ArrayList<Ordencompra> listaResultados = new ArrayList<>();       
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = session.beginTransaction();
        try{
            List lista = session.createQuery("from Ordencompra where idordencompra ="+ id +"").list();        
            Iterator iter = lista.iterator();
            while (iter.hasNext()) {    
                Ordencompra p = (Ordencompra) iter.next();
                Hibernate.initialize(p.getCodigo());
                Hibernate.initialize(p.getEstado());
                Hibernate.initialize(p.getFecharecepcion());                
                Hibernate.initialize(p.getFechasolicitud());
                Hibernate.initialize(p.getIdordencompra());                
                Hibernate.initialize(p.getLineaordencompras()); 
                Hibernate.initialize(p.getMoneda());                
                Hibernate.initialize(p.getMontototal());
                Hibernate.initialize(p.getProveedor());
                 Hibernate.initialize(p.getProveedor().getRazonsocial());           
                listaResultados.add(p);
            }        
            tx.commit();
        } catch(HibernateException e) {
            throw new HibernateException(e);    
        } finally {
            session.close();
            return listaResultados;
        }         
    }   
    
      public ArrayList<Ordencompra> listarOrden()
    {
        ArrayList<Ordencompra> listaResultados = new ArrayList<>();       
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = session.beginTransaction();
        try{
            List lista = session.createQuery("from Ordencompra order by idordencompra").list();        
            Iterator iter = lista.iterator();
            while (iter.hasNext()) {    
                Ordencompra p = (Ordencompra) iter.next();
                Hibernate.initialize(p.getCodigo());
                Hibernate.initialize(p.getEstado());
                Hibernate.initialize(p.getFecharecepcion());                
                Hibernate.initialize(p.getFechasolicitud());
                Hibernate.initialize(p.getIdordencompra());                
                Hibernate.initialize(p.getLineaordencompras()); 
                Hibernate.initialize(p.getMoneda());                
                Hibernate.initialize(p.getMontototal());
                Hibernate.initialize(p.getProveedor());
                Hibernate.initialize(p.getPedidocompra());
                Hibernate.initialize(p.getProveedor().getRazonsocial());  
                listaResultados.add(p);
            }        
            tx.commit();
        } catch(HibernateException e) {
            throw new HibernateException(e);    
        } finally {
            session.close();
            return listaResultados;
        }         
    }   
      
       public ArrayList<Simulacion> listarSimulacion(int id)
    {
        ArrayList<Simulacion> listaResultados = new ArrayList<>();       
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = session.beginTransaction();
        try{
            List lista = session.createQuery("from Simulacion where idpedidocompra = '"+ id +"' order by idpedidocompra").list();        
            Iterator iter = lista.iterator();
            while (iter.hasNext()) {    
                Simulacion p = (Simulacion) iter.next();
                Hibernate.initialize(p.getIdsimulacion());
                Hibernate.initialize(p.getEstado());
                Hibernate.initialize(p.getFechaHora());                
                Hibernate.initialize(p.getProduccion());
                Hibernate.initialize(p.getEstado());                
                Hibernate.initialize(p.getPedidocompra().getIdpedidocompra()); 
                Hibernate.initialize(p.getCantdecorada()); 
                Hibernate.initialize(p.getCantnormal()); 
                Hibernate.initialize(p.getCantrellena());
                Hibernate.initialize(p.getDemandas());
                Hibernate.initialize(p.getEmpleadoxmaquinasimulacions()); 
                Hibernate.initialize(p.getTurno()); 
                Hibernate.initialize(p.getProductosimulacions());
                Hibernate.initialize(p.getPedidoproduccions());
                
                listaResultados.add(p);
            }        
            tx.commit();
        } catch(HibernateException e) {
            throw new HibernateException(e);    
        } finally {
            session.close();
            return listaResultados;
        }         
    }   
    
      
      public ArrayList<Pedidocompra> listarPedidoCompraPorFecha(Date fechai, Date fechaf)
    {
        ArrayList<Pedidocompra> listaResultados = new ArrayList<>();       
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = session.beginTransaction();
        try{
            List lista = session.createQuery("from Pedidocompra where Fechasolicitud between '" + fechai +"' and '" + fechaf + "' order by Idpedidocompra").list();        
            Iterator iter = lista.iterator();
            while (iter.hasNext()) {    
                Pedidocompra p = (Pedidocompra) iter.next();
                Hibernate.initialize(p.getCodigo());
                Hibernate.initialize(p.getEstado());
                Hibernate.initialize(p.getFechasolicitud());
                Hibernate.initialize(p.getIdpedidocompra());
                Hibernate.initialize(p.getLineapedidocompras());
                Hibernate.initialize(p.getMotivo());
                
                listaResultados.add(p);
            }        
            tx.commit();
        } catch(HibernateException e) {
            throw new HibernateException(e);    
        } finally {
            session.close();
            return listaResultados;
        }         
    }
    
      public ArrayList<Pedidocompra> listarPedidoCompraPorCodigo(String codigo)
    {
        ArrayList<Pedidocompra> listaResultados = new ArrayList<>();       
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = session.beginTransaction();
        try{
            List lista = session.createQuery("from Pedidocompra where codigo = '" + codigo + "' order by Idpedidocompra").list();        
            Iterator iter = lista.iterator();
            while (iter.hasNext()) {    
                Pedidocompra p = (Pedidocompra) iter.next();
                Hibernate.initialize(p.getCodigo());
                Hibernate.initialize(p.getEstado());
                Hibernate.initialize(p.getFechasolicitud());
                Hibernate.initialize(p.getIdpedidocompra());
                Hibernate.initialize(p.getLineapedidocompras());
                Hibernate.initialize(p.getMotivo());
                
                listaResultados.add(p);
            }        
            tx.commit();
        } catch(HibernateException e) {
            throw new HibernateException(e);    
        } finally {
            session.close();
            return listaResultados;
        }         
    }
    
      public ArrayList<Pedidocompra> listarPedidoPorEstado(int estado)
    {
        ArrayList<Pedidocompra> listaResultados = new ArrayList<>();       
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = session.beginTransaction();
        try{
            List lista = session.createQuery("from Pedidocompra where estado = " + estado + " order by Idpedidocompra").list();        
            Iterator iter = lista.iterator();
            while (iter.hasNext()) {    
                Pedidocompra p = (Pedidocompra) iter.next();
                Hibernate.initialize(p.getCodigo());
                Hibernate.initialize(p.getEstado());
                Hibernate.initialize(p.getFechasolicitud());
                Hibernate.initialize(p.getIdpedidocompra());
                Hibernate.initialize(p.getLineapedidocompras());
                Hibernate.initialize(p.getMotivo());
                
                listaResultados.add(p);
            }        
            tx.commit();
        } catch(HibernateException e) {
            throw new HibernateException(e);    
        } finally {
            session.close();
            return listaResultados;
        }         
    }
      
      public ArrayList<Pedidocompra> listarPedidoCompra()
    {
        ArrayList<Pedidocompra> listaResultados = new ArrayList<>();       
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = session.beginTransaction();
        try{
            List lista = session.createQuery("from Pedidocompra order by Idpedidocompra").list();        
            Iterator iter = lista.iterator();
            while (iter.hasNext()) {    
                Pedidocompra p = (Pedidocompra) iter.next();
                Hibernate.initialize(p.getCodigo());
                Hibernate.initialize(p.getEstado());
                Hibernate.initialize(p.getFechasolicitud());
                Hibernate.initialize(p.getIdpedidocompra());
                Hibernate.initialize(p.getLineapedidocompras());
                Hibernate.initialize(p.getMotivo());
                Hibernate.initialize(p.getNumordcomprafinalizadas());
                Hibernate.initialize(p.getNumordcompratotal());
                listaResultados.add(p);
            }        
            tx.commit();                                
        } catch(HibernateException e) {
            throw new HibernateException(e);  
      
        } finally {
            session.close();
            return listaResultados;
        }         
    }
      
      public ArrayList<Lineapedidocompra> llenarDetallePedido(Pedidocompra u)
    {
        ArrayList<Lineapedidocompra> listaResultados = new ArrayList<>();       
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = session.beginTransaction();
        try{
            List lista = session.createQuery("from Lineapedidocompra where idpedidocompra="+u.getIdpedidocompra()+"").list();        
            Iterator iter = lista.iterator();
            while (iter.hasNext()) {    
                Lineapedidocompra p = (Lineapedidocompra) iter.next();
                Hibernate.initialize(p.getCantidad());
                Hibernate.initialize(p.getCosto());
                Hibernate.initialize(p.getIdlineapedidocompra());

                Hibernate.initialize(p.getPedidocompra());
                Hibernate.initialize(p.getMoneda().getSimbolo());
                Hibernate.initialize(p.getPedidocompra());
                Hibernate.initialize(p.getProveedor());
                Hibernate.initialize(p.getTipoproducto());
                
                listaResultados.add(p);
            }        
            tx.commit();
        } catch(HibernateException e) {
            throw new HibernateException(e);    
        } finally {
            session.close();
            return listaResultados;
        }         
    }
    public ArrayList<Costoinsumos> llenarCostos()
    {
        ArrayList<Costoinsumos> listaResultados = new ArrayList<>();       
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = session.beginTransaction();
        try{
            List lista = session.createQuery("from Costoinsumos order by idtipoproducto,costo ASC").list();        
            Iterator iter = lista.iterator();
            while (iter.hasNext()) {    
                Costoinsumos p = (Costoinsumos) iter.next();
                Hibernate.initialize(p.getCosto());
                Hibernate.initialize(p.getFechafin());
                Hibernate.initialize(p.getFechainicio());

                Hibernate.initialize(p.getIdcostoinsumos());
                Hibernate.initialize(p.getMoneda());
                Hibernate.initialize(p.getProveedor().getEstado());
                Hibernate.initialize(p.getTipoproducto());
                listaResultados.add(p);
            }        
            tx.commit();
         
        } catch(HibernateException e) {
            throw new HibernateException(e);    
        } finally {
            session.close();
            return listaResultados;
        }         
    }
    
    public ArrayList<Lineaordencompra> listarLineaOrdenCompra(Ordencompra u)
    {
        ArrayList<Lineaordencompra> listaResultados = new ArrayList<>();       
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = session.beginTransaction();
        try{
            List lista = session.createQuery("from Lineaordencompra where idordencompra ="+u.getIdordencompra()+"").list();        
            Iterator iter = lista.iterator();
            while (iter.hasNext()) {    
                Lineaordencompra p = (Lineaordencompra) iter.next();
                Hibernate.initialize(p.getCantidadEntrante());
                Hibernate.initialize(p.getCantidadFaltante());
                Hibernate.initialize(p.getCantidadSolicitante());
                Hibernate.initialize(p.getFechaCaducidad());
                Hibernate.initialize(p.getIdlineaordencompra());
                Hibernate.initialize(p.getLineapedidocompra());
                Hibernate.initialize(p.getLineapedidocompra().getTipoproducto());
                Hibernate.initialize(p.getLineapedidocompra().getTipoproducto().getNombre());
                Hibernate.initialize(p.getOrdencompra().getMoneda());
                listaResultados.add(p);
            }        
            tx.commit();
         
        } catch(HibernateException e) {
            throw new HibernateException(e);    
        } finally {
            session.close();
            return listaResultados;
        }         
    }
    
     public Ordencompra sacarUltCodPedido()
    {
        int cont=0;
        ArrayList<Ordencompra> listaResultados = new ArrayList<>();       
        
        //String lista=null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = session.beginTransaction();
        try{
            List lista = session.createQuery("from Ordencompra order by Idordencompra").list();        
            Iterator iter = lista.iterator();
            
            while (iter.hasNext()) {    
                Ordencompra p = (Ordencompra) iter.next();
                Hibernate.initialize(p.getCodigo());
               
                //Hibernate.initialize(p.getTipo());
                cont=cont+1;
                listaResultados.add(p); 
            }
            tx.commit();
            
            } catch(HibernateException e) {
            throw new HibernateException(e);  
        }finally {
            session.close();
            if (cont==0){
                Ordencompra cmpNuevo=new Ordencompra();
                cmpNuevo.setCodigo("CMP00000000");
      
                return cmpNuevo;
            }
            else{
                return (listaResultados.get(cont-1));
            }    
        }           
    } 
 
    public String llenaCodigoOrden(Ordencompra e){

        Ordencompra ord = sacarUltCodPedido();
        String codigo=ord.getCodigo();
        //aca saca el codigo mas grande  PED00000001
        if (e!=null) {
            return codigo;
        }
        if (ord.getCodigo().equals("CMP00000000")){
          return "CMP00000001";
        }
        
        else{
            String ceros="";
            //Genero Codigo Siguiente
            String ped= codigo.substring(0, 3);
            String num=codigo.substring(3, 11);
            int longC=num.length(); //aca siempre es 8

            int numero=Integer.parseInt(num);
            numero++;

            String nume=String.valueOf(numero);
            //saco longitud de ese entero en caracteres
            int longE=nume.length();

            for(int i=1;i<(longC-longE+1);i++){
                ceros=ceros+'0';
            }

            codigo= ped+""+ceros+""+numero;
           return codigo;
        }
         
    }
    public ArrayList<Moneda> listarMoneda(){
        ArrayList<Moneda> monedas = new ArrayList<>();
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = session.beginTransaction();
        try{
            List lista = session.createQuery("from Moneda order by Idmoneda").list();        
            Iterator iter = lista.iterator();
            while (iter.hasNext()) {    
                Moneda m = (Moneda) iter.next();
                
                Hibernate.initialize(m.getIdmoneda());
                                
                monedas.add(m);
            }        
            tx.commit();
        } catch(HibernateException e) {
            throw new HibernateException(e);    
        } finally {
            session.close();
            return monedas;
        }     
    }
       
       public Moneda sacaMoneda(){
        ArrayList<Moneda> listaResultados = new ArrayList<>();       
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = session.beginTransaction();
        try{
            List lista = session.createQuery("from Moneda where idmoneda=1").list();        
            Iterator iter = lista.iterator();
            while (iter.hasNext()) {    
                Moneda p = (Moneda) iter.next();
                Hibernate.initialize(p.getIdmoneda());
                Hibernate.initialize(p.getSimbolo());
                Hibernate.initialize(p.getNombre());
                Hibernate.initialize(p.getDescripcion());
                Hibernate.initialize(p.getEstado());
                
                
                listaResultados.add(p);
            }        
            tx.commit();
        } catch(HibernateException e) {
            throw new HibernateException(e);    
        } finally {
            session.close();
            return listaResultados.get(0);
        }         
}
       
        public Materiaprima sacarMateriaPrima(int cod){
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = session.beginTransaction();
        try{
            
             Materiaprima c = new Materiaprima();      
            List lista = session.createQuery("from Materiaprima where idtipoproducto="+cod).list();        
            Iterator iter = lista.iterator();
            while (iter.hasNext()) { 
                c = (Materiaprima) iter.next();
                Hibernate.initialize(c.getIdmateriaprima());
                Hibernate.initialize(c.getTipoproducto());
                Hibernate.initialize(c.getTipoproducto().getIdtipoproducto());
                Hibernate.initialize(c.getTipoproducto().getNombre());
                Hibernate.initialize(c.getAlmacen());
                Hibernate.initialize(c.getAlmacen().getIdalmacen());
                Hibernate.initialize(c.getCantidad());
               
            }        
            tx.commit();
           
            return c;
        }catch(HibernateException e){
            throw new HibernateException(e);
        }finally{
            session.close();
        }
    
      }
        
         public Tipoproducto sacarTipoProducto (int cod){
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = session.beginTransaction();
        try{
            
             Tipoproducto c = new Tipoproducto();      
            List lista = session.createQuery("from Tipoproducto where idtipoproducto="+cod).list();        
            Iterator iter = lista.iterator();
            while (iter.hasNext()) { 
                c = (Tipoproducto) iter.next();
                Hibernate.initialize(c.getIdtipoproducto());
                Hibernate.initialize(c.getNombre());
                Hibernate.initialize(c.getEstado());
               
            }        
            tx.commit();
           
            return c;
        }catch(HibernateException e){
            throw new HibernateException(e);
        }finally{
            session.close();
        }
    }
         
         public Pedidocompra sacarPedidoCompra (int cod){
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = session.beginTransaction();
        try{
            
             Pedidocompra c = new Pedidocompra();      
            List lista = session.createQuery("from Pedidocompra where idpedidocompra="+cod).list();        
            Iterator iter = lista.iterator();
            while (iter.hasNext()) { 
                c = (Pedidocompra) iter.next();
                Hibernate.initialize(c.getIdpedidocompra());
                Hibernate.initialize(c.getEstado());
                Hibernate.initialize(c.getNumordcomprafinalizadas());
                Hibernate.initialize(c.getNumordcompratotal());
                Hibernate.initialize(c.getLineapedidocompras());
            }        
            tx.commit();
           
            return c;
        }catch(HibernateException e){
            throw new HibernateException(e);
        }finally{
            session.close();
        }
    }
         
          public void guardarMateriaPrima(Materiaprima materiaPrima){
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction tx =  session.beginTransaction();
        try {
            session.saveOrUpdate(materiaPrima);
            tx.commit();
        }catch(HibernateException e) {
            throw new HibernateException(e);
        } finally {
            session.close();            
        }        
    }
          
           public void guardarKardex(Kardex kardex){
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction tx =  session.beginTransaction();
        try {
            session.saveOrUpdate(kardex);
            tx.commit();
        }catch(HibernateException e) {
            throw new HibernateException(e);
        } finally {
            session.close();            
        }  }
           
           
}


